﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for CommentDAL
/// </summary>
public class CommentDAL : DALBase
{

    #region CREATE


    public void NewComment(Comment comment)
    {
        using (SqlConnection conn = CreateConnection()) 
        {
            try
            {       
                var cmd = new SqlCommand("dbo.usp_CreateComment", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@CommentID", SqlDbType.Int, 4).Direction = ParameterDirection.Output;

                cmd.Parameters.Add("@TopicID", SqlDbType.Int, 4).Value = comment.TopicID;
                cmd.Parameters.Add("@TextContent", SqlDbType.VarChar, 2000).Value = comment.TextContent;
                cmd.Parameters.Add("@UserID", SqlDbType.Int, 4).Value = comment.UserID;
               


                conn.Open(); 
                cmd.ExecuteNonQuery();
                comment.TopicID = (Int32)cmd.Parameters["@CommentID"].Value;  
            }
            catch
            {
                throw new ApplicationException("An error has occured in the data access layer. ");
            }

        }
    }


     

    #endregion

    #region READ

    

    public List<Comment> GetAllCommentsOnTopicID(int topicID)
    {
        Sanitize sanitize = new Sanitize();
        var comments = new List<Comment>();
        using (SqlConnection conn = CreateConnection())
        {
            try
            {
                SqlCommand cmd = new SqlCommand("dbo.usp_GetAllCommentsOnTopicID", conn);
                cmd.CommandType = CommandType.StoredProcedure;


                cmd.Parameters.AddWithValue("@TopicID", topicID);

                conn.Open();


                using (SqlDataReader reader = cmd.ExecuteReader())
                {

                    while (reader.Read())
                    {

                        var TextContentIndex = reader.GetOrdinal("TextContent");
                        var DateIndex = reader.GetOrdinal("TheDate");
                        var AuthorIndex = reader.GetOrdinal("Username");
                        var CommentIDIndex = reader.GetOrdinal("CommentID");
                        var UserIDIndex = reader.GetOrdinal("UserID");

                        comments.Add( new Comment
                        {
                            TopicID = topicID,
                            TextContent = reader.GetString(TextContentIndex),
                            Date = sanitize.DecodeText(String.Format("{0}", reader.GetDateTime(DateIndex))),
                            Author = sanitize.DecodeText(reader.GetString(AuthorIndex)),
                            UserID = reader.GetInt32(UserIDIndex),
                            CommentID = reader.GetInt32(CommentIDIndex),
                        });
                    }
                }
                return comments;
            }
            catch
            {
                throw new ApplicationException("an error has occured in the data access layer");
            }
        }
    }


    #endregion


    #region UPDATE

    #endregion

    #region DELETE

    public void DeleteCommentOnID(int commentID)
    {
        using (SqlConnection conn = CreateConnection()) // using för att den alltid ska stängas sen.
        {
            try
            {
                var cmd = new SqlCommand("dbo.usp_UpdateCommentToDeleted", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@CommentID", SqlDbType.Int, 4).Value = commentID;
                cmd.Parameters.Add("@Deleted", SqlDbType.VarChar, 5).Value = "true";

                conn.Open();
                cmd.ExecuteNonQuery();
            }
            catch
            {
                throw new ApplicationException("An error has occured in the data access layer.");
            }
        }
    }

  

    #endregion







 
}